package com.zxl.service.impl;

import com.zxl.mapper.DaDevFlowLinkMapper;
import com.zxl.model.DaDevFlowLink;
import com.zxl.service.DaDevFlowLinkService;
import com.zxl.support.DirectedGraph;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class DaDevFlowLinkServiceImpl implements DaDevFlowLinkService {
    @Resource
    private DaDevFlowLinkMapper daDevFlowLinkMapper;

    @Override
    public Boolean checkHasCycle(Integer flowId) {
        List<DaDevFlowLink> links = daDevFlowLinkMapper.selectAllByFlowId(flowId);
        DirectedGraph graph = new DirectedGraph(links.size());

        // 假设这些节点是从数据库中查询的结果
        links.forEach(daDevFlowLink -> {
            graph.addEdge(daDevFlowLink.getFromFlowId(), daDevFlowLink.getToFlowId());
        });
        log.info("hasCycle：{}", graph.hasCycle());
        return graph.hasCycle();
    }
}
